package com.example.xiaodong.cn.dajiqi.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xiaodong.cn.dajiqi.R;

import java.util.List;

public class TextAdapter extends BaseAdapter {

    private List<JavaBean> lists;
    private Context        mContext;
    private RelativeLayout layout;

    public TextAdapter(List<JavaBean> lists, Context mContext) {

        this.lists = lists;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        //判断如果是接受到系统的就加载左边布局
        if (lists.get(position).getFlag() == JavaBean.RECEIVER) {
            layout = (RelativeLayout) inflater.inflate(R.layout.leftitem, null);
            if (layout == null){

            }
        }
        //判断如果是自己发送的，就加载右边布局
        if (lists.get(position).getFlag() == JavaBean.SEND) {
            layout = (RelativeLayout) inflater.inflate(R.layout.rightitem, null);
        }
        //加载的TextView，既是左边布局的，又是右边布局的，左边和右边的id一样
        TextView tv = (TextView) layout.findViewById(R.id.tv);
        tv.setText(lists.get(position).getContent());
        TextView time = (TextView) layout.findViewById(R.id.time);
        time.setText(lists.get(position).getTime());
        return layout;
    }
}
